package Consumer;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.ArrayList;

public class MyListener_err implements MessageListener {
    int num = 0;
    double var = 0;
    ArrayList<Double> list = new ArrayList<>();
    double sum = 0;
    int errnum = 0;
    @Override
    public void onMessage(Message message) {
        try {
            num++;
            double number = Double.valueOf(((TextMessage)message).getText());
            list.add(number);
            sum += number;
//            System.out.println("Received a message: "+((TextMessage)message).getText());
            if(num >= 1){
                double mean = sum/num;
                for (double m: list) {
                    var += (m-mean) * (m-mean);
                }
                var /= num;
                double uplim = mean + 3 * var;
                double downlim = mean - 3 * var;
                if(number > uplim || number < downlim){
                    errnum++;
                }
                System.out.println(num+"个数中:"+"异常点个数为："+ errnum);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
